/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.commons.ui;

import java.awt.Component;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/**
 * Helper for select file(s) or directory using {@link JFileChooser} dialog.
 */
public class FileChooser {

    private FileChooser() {
    }

    /**
     * Selects directory.
     * @param owner owner component.
     * @param initialDir initial directory.
     * @return selected directory or null if no directory was selected (user
     * cancels dialog).
     */
    public static File selectDirectory(Component owner, File initialDir){
        JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new FileFilter() {
            @Override
            public boolean accept(File f) {
                return f.isDirectory();
            }
            @Override
            public String getDescription() {
                return "Directory";
            }
        });
        fc.setCurrentDirectory(initialDir);
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int result = fc.showOpenDialog(owner);

        if(result != JFileChooser.APPROVE_OPTION){
            return null;
        }
        File directory = fc.getSelectedFile();
        return directory;
    }

}
